import os;
import time;

def sendmail(reportAddr, path):
    SENDMAIL = "/usr/sbin/sendmail" # sendmail location
    p = os.popen("%s -t" % SENDMAIL, "w")
    p.write("To: " + reportAddr + '\n')
    p.write("Subject: Warning from blog crawler\n")
    p.write("\n") # blank line separating headers from body
    p.write(path + "has not updated for a long time, so it seems something unfortunately happened. \n")
    p.write("Please check it!\n")
    sts = p.close()
   

def detect(paths, detectInterval, senseInterval, reportAddr):
    while 1:
        delPaths = [];
        for path in paths:
            t1 = time.time();
            t2 = os.path.getmtime(path);
            if abs(t1 - t2) > senseInterval:
                sendmail(reportAddr, path);
                delPaths.append(path); 
        for path in delPaths:
            paths.remove(path);
        if delPaths.__len__() == 0:
            print time.asctime(),": normal";
        else:
            print time.asctime(),": false at", delPaths;
        time.sleep(detectInterval);


if __name__ == '__main__':
    import sys;
    mailAdress = sys.argv[1];
    paths = sys.argv[2:];
    detect(paths, 1000, 1000, mailAddress);
